import SwiftUI

struct ExploreDetailView: View {
    let plan: TrainingPlan
    @State private var selectedSection: Int = 0
    
    var body: some View {
        ScrollView {
            VStack(spacing: 0) {
                // 封面图
                ZStack(alignment: .bottomLeading) {
                    Image("plan_cover") // 使用实际的封面图
                        .resizable()
                        .aspectRatio(contentMode: .fill)
                        .frame(height: 200)
                        .clipped()
                    
                    LinearGradient(
                        gradient: Gradient(colors: [.black.opacity(0.5), .clear]),
                        startPoint: .bottom,
                        endPoint: .top
                    )
                    .frame(height: 100)
                    
                    VStack(alignment: .leading, spacing: 8) {
                        Text(plan.title)
                            .font(.title2)
                            .bold()
                            .foregroundColor(.white)
                        
                        HStack {
                            Label("\(plan.duration)天", systemImage: "clock")
                            Label("\(plan.difficulty)", systemImage: "star.fill")
                            Label("\(plan.participants)人参与", systemImage: "person.2")
                        }
                        .font(.caption)
                        .foregroundColor(.white)
                    }
                    .padding()
                }
                
                // 分段控制器
                Picker("Section", selection: $selectedSection) {
                    Text("介绍").tag(0)
                    Text("课程").tag(1)
                    Text("评价").tag(2)
                }
                .pickerStyle(SegmentedPickerStyle())
                .padding()
                
                // 内容区域
                Group {
                    switch selectedSection {
                    case 0:
                        planIntroduction
                    case 1:
                        planLessons
                    case 2:
                        planReviews
                    default:
                        EmptyView()
                    }
                }
            }
        }
        .navigationBarTitleDisplayMode(.inline)
        .toolbar {
            ToolbarItem(placement: .navigationBarTrailing) {
                Button(action: {}) {
                    Image(systemName: "square.and.arrow.up")
                }
            }
        }
    }
    
    private var planIntroduction: some View {
        VStack(alignment: .leading, spacing: 16) {
            // 适合人群
            VStack(alignment: .leading, spacing: 8) {
                Text("适合人群")
                    .font(.headline)
                
                Text(plan.suitableFor)
                    .foregroundColor(.secondary)
            }
            
            // 学习目标
            VStack(alignment: .leading, spacing: 8) {
                Text("学习目标")
                    .font(.headline)
                
                ForEach(plan.learningGoals, id: \.self) { goal in
                    HStack(alignment: .top, spacing: 8) {
                        Image(systemName: "checkmark.circle.fill")
                            .foregroundColor(.green)
                        Text(goal)
                    }
                }
            }
            
            // 课程特点
            VStack(alignment: .leading, spacing: 8) {
                Text("课程特点")
                    .font(.headline)
                
                ForEach(plan.features, id: \.self) { feature in
                    HStack(alignment: .top, spacing: 12) {
                        Image(systemName: feature.icon)
                            .foregroundColor(.blue)
                            .frame(width: 24, height: 24)
                        
                        VStack(alignment: .leading, spacing: 4) {
                            Text(feature.title)
                                .font(.subheadline)
                                .bold()
                            Text(feature.description)
                                .font(.subheadline)
                                .foregroundColor(.secondary)
                        }
                    }
                }
            }
            
            // 加入按钮
            Button(action: {}) {
                Text("加入修炼计划")
                    .font(.headline)
                    .foregroundColor(.white)
                    .frame(maxWidth: .infinity)
                    .padding()
                    .background(Color.blue)
                    .cornerRadius(10)
            }
            .padding(.top)
        }
        .padding()
    }
    
    private var planLessons: some View {
        VStack(spacing: 0) {
            ForEach(plan.lessons) { lesson in
                VStack(spacing: 0) {
                    Button(action: {}) {
                        HStack(spacing: 16) {
                            Text("\(lesson.day)")
                                .font(.headline)
                                .foregroundColor(.white)
                                .frame(width: 36, height: 36)
                                .background(Color.blue)
                                .clipShape(Circle())
                            
                            VStack(alignment: .leading, spacing: 4) {
                                Text(lesson.title)
                                    .font(.headline)
                                    .foregroundColor(.primary)
                                
                                Text("\(lesson.duration)分钟 · \(lesson.type)")
                                    .font(.subheadline)
                                    .foregroundColor(.secondary)
                            }
                            
                            Spacer()
                            
                            Image(systemName: "play.circle.fill")
                                .foregroundColor(.blue)
                                .font(.title2)
                        }
                        .padding()
                    }
                    
                    Divider()
                }
            }
        }
    }
    
    private var planReviews: some View {
        VStack(spacing: 16) {
            // 评分统计
            HStack(spacing: 20) {
                VStack(spacing: 8) {
                    Text("4.9")
                        .font(.system(size: 40, weight: .bold))
                    Text("综合评分")
                        .font(.caption)
                        .foregroundColor(.secondary)
                }
                
                VStack(alignment: .leading, spacing: 4) {
                    ForEach((1...5).reversed(), id: \.self) { stars in
                        HStack(spacing: 8) {
                            Text("\(stars)星")
                                .font(.caption)
                                .frame(width: 30, alignment: .leading)
                            
                            GeometryReader { geometry in
                                RoundedRectangle(cornerRadius: 2)
                                    .fill(Color.blue.opacity(0.2))
                                    .frame(height: 4)
                                    .overlay(
                                        RoundedRectangle(cornerRadius: 2)
                                            .fill(Color.blue)
                                            .frame(width: geometry.size.width * CGFloat(stars) / 5)
                                    )
                            }
                            .frame(height: 4)
                            
                            Text("\(Int(Double(stars) * 20))%")
                                .font(.caption)
                                .frame(width: 40, alignment: .trailing)
                        }
                    }
                }
            }
            .padding()
            .background(Color(.systemBackground))
            .cornerRadius(10)
            
            // 用户评价
            ForEach(plan.reviews) { review in
                VStack(alignment: .leading, spacing: 12) {
                    HStack {
                        Image(systemName: "person.circle.fill")
                            .font(.title2)
                            .foregroundColor(.blue)
                        
                        VStack(alignment: .leading, spacing: 4) {
                            Text(review.author)
                                .font(.headline)
                            Text(review.date)
                                .font(.caption)
                                .foregroundColor(.secondary)
                        }
                        
                        Spacer()
                        
                        HStack(spacing: 4) {
                            ForEach(0..<5) { index in
                                Image(systemName: index < review.rating ? "star.fill" : "star")
                                    .foregroundColor(.yellow)
                            }
                        }
                    }
                    
                    Text(review.content)
                        .font(.subheadline)
                    
                    if !review.images.isEmpty {
                        ScrollView(.horizontal, showsIndicators: false) {
                            HStack(spacing: 8) {
                                ForEach(review.images, id: \.self) { image in
                                    Image(image)
                                        .resizable()
                                        .aspectRatio(contentMode: .fill)
                                        .frame(width: 80, height: 80)
                                        .clipShape(RoundedRectangle(cornerRadius: 8))
                                }
                            }
                        }
                    }
                    
                    HStack(spacing: 16) {
                        Button(action: {}) {
                            HStack {
                                Image(systemName: "hand.thumbsup")
                                Text("\(review.likes)")
                            }
                        }
                        
                        Button(action: {}) {
                            HStack {
                                Image(systemName: "bubble.left")
                                Text("\(review.comments)")
                            }
                        }
                    }
                    .font(.caption)
                    .foregroundColor(.secondary)
                }
                .padding()
                .background(Color(.systemBackground))
                .cornerRadius(10)
            }
        }
        .padding()
    }
}

// 模型
struct TrainingPlan: Identifiable {
    let id = UUID()
    let title: String
    let duration: Int
    let difficulty: String
    let participants: Int
    let suitableFor: String
    let learningGoals: [String]
    let features: [PlanFeature]
    let lessons: [Lesson]
    let reviews: [Review]
}

struct PlanFeature: Identifiable, Hashable {
    let id: UUID
    let icon: String
    let title: String
    let description: String
    
    // Implement Hashable
    func hash(into hasher: inout Hasher) {
        hasher.combine(id)
    }
    
    static func == (lhs: PlanFeature, rhs: PlanFeature) -> Bool {
        lhs.id == rhs.id
    }
}

struct Lesson: Identifiable {
    let id = UUID()
    let day: Int
    let title: String
    let duration: Int
    let type: String
}

struct Review: Identifiable {
    let id = UUID()
    let author: String
    let date: String
    let rating: Int
    let content: String
    let images: [String]
    let likes: Int
    let comments: Int
}

// 预览数据
extension TrainingPlan {
    static let preview = TrainingPlan(
        title: "基础剑法入门",
        duration: 30,
        difficulty: "入门",
        participants: 1234,
        suitableFor: "适合零基础学习者，想要系统学习剑法的修炼者",
        learningGoals: [
            "掌握基本剑法要诀",
            "建立正确的修炼心法",
            "形成良好的修炼习惯"
        ],
        features: [
            PlanFeature(id: UUID(), icon: "book.fill", title: "系统教学", description: "循序渐进的课程体系"),
            PlanFeature(id: UUID(), icon: "person.fill", title: "名师指导", description: "资深导师全程跟进"),
            PlanFeature(id: UUID(), icon: "video.fill", title: "视频讲解", description: "高清视频详细演示")
        ],
        lessons: [
            Lesson(day: 1, title: "基础站姿与握剑", duration: 30, type: "基础课程"),
            Lesson(day: 2, title: "剑法基本动作", duration: 45, type: "基础课程"),
            Lesson(day: 3, title: "剑气运行要诀", duration: 60, type: "进阶课程")
        ],
        reviews: [
            Review(
                author: "修炼者",
                date: "2024-01-13",
                rating: 5,
                content: "非常系统的入门课程，老师讲解很细致，每天跟着练习都能感受到进步。",
                images: [],
                likes: 23,
                comments: 5
            )
        ]
    )
}

